--[[
    Init.lua用来初始化任务中的各个模块

]]

--取消报错弹窗
env.setErrorMessageBoxEnabled(false)

--- 获取地图信息
local theatre = env.mission.theatre

---初始化AARTanker
do
    if theatre == 'GermanyCW' then
        AARTanker.setAirspaceBasePoint({
            x = -77214,
            y = 0,
            z = -863001,
        })

        AARTanker.setDefaultTrackHeading(96)
    end

    AARTanker:New('S-3B Tanker')
    AARTanker:New('KC135MPRS')
    AARTanker:New('KC-135')
    AARTanker:New('IL-78M')
    AARTanker:New('KC130')
    AARTanker:New('KC-135')
    AARTanker:New('KC135MPRS')
end

---初始化AGRange
do
    --[[
        运行流程:
        1. 作者使用DCS的triggerZone划定一个 长方形 靶场区域 总面积
        2. 每个 小靶场 均为正方形, 作者需要确定 正方形区域 的 边长
    ]]

    -- local ZoneMax = Utils.getZone('New Trigger Zone-1')
    local PlanRangeZone = {
        verticies = {
            [1] = coord.LLtoLO(coord.MGRStoLL({
                UTMZone = '37T',
                MGRSDigraph = 'DK',
                Easting = 50000,
                Northing = 60000,
            })),
            [2] = coord.LLtoLO(coord.MGRStoLL({
                UTMZone = '37T',
                MGRSDigraph = 'EK',
                Easting = 00000,
                Northing = 60000,
            })),
            [3] = coord.LLtoLO(coord.MGRStoLL({
                UTMZone = '37T',
                MGRSDigraph = 'EL',
                Easting = 00000,
                Northing = 10000,
            })),
            [4] = coord.LLtoLO(coord.MGRStoLL({
                UTMZone = '37T',
                MGRSDigraph = 'DL',
                Easting = 50000,
                Northing = 10000,
            })),
        }
    }

    local HeliRangeZone = {
        verticies = {
            [1] = coord.LLtoLO(coord.MGRStoLL({
                UTMZone = '37T',
                MGRSDigraph = 'EL',
                Easting = 10000,
                Northing = 10000,
            })),
            [2] = coord.LLtoLO(coord.MGRStoLL({
                UTMZone = '37T',
                MGRSDigraph = 'EL',
                Easting = 30000,
                Northing = 10000,
            })),
            [3] = coord.LLtoLO(coord.MGRStoLL({
                UTMZone = '37T',
                MGRSDigraph = 'EL',
                Easting = 30000,
                Northing = 30000,
            })),
            [4] = coord.LLtoLO(coord.MGRStoLL({
                UTMZone = '37T',
                MGRSDigraph = 'EL',
                Easting = 10000,
                Northing = 30000,
            })),
        }
    }

    if theatre == 'GermanyCW' then
        PlanRangeZone = {
            verticies = {
                [1] = coord.LLtoLO(coord.MGRStoLL({
                    UTMZone = '32U',
                    MGRSDigraph = 'MD',
                    Easting = 00000,
                    Northing = 00000,
                })),
                [2] = coord.LLtoLO(coord.MGRStoLL({
                    UTMZone = '32U',
                    MGRSDigraph = 'MD',
                    Easting = 50000,
                    Northing = 00000,
                })),
                [3] = coord.LLtoLO(coord.MGRStoLL({
                    UTMZone = '32U',
                    MGRSDigraph = 'MD',
                    Easting = 50000,
                    Northing = 50000,
                })),
                [4] = coord.LLtoLO(coord.MGRStoLL({
                    UTMZone = '32U',
                    MGRSDigraph = 'MD',
                    Easting = 00000,
                    Northing = 50000,
                })),
            }
        }

        HeliRangeZone = {
            verticies = {
                [1] = coord.LLtoLO(coord.MGRStoLL({
                    UTMZone = '32U',
                    MGRSDigraph = 'MC',
                    Easting = 20000,
                    Northing = 50000,
                })),
                [2] = coord.LLtoLO(coord.MGRStoLL({
                    UTMZone = '32U',
                    MGRSDigraph = 'MC',
                    Easting = 40000,
                    Northing = 50000,
                })),
                [3] = coord.LLtoLO(coord.MGRStoLL({
                    UTMZone = '32U',
                    MGRSDigraph = 'MC',
                    Easting = 40000,
                    Northing = 70000,
                })),
                [4] = coord.LLtoLO(coord.MGRStoLL({
                    UTMZone = '32U',
                    MGRSDigraph = 'MC',
                    Easting = 20000,
                    Northing = 70000,
                })),
            }
        }
    end

    --Debug
    local function MarkTotalZone(totalZone)
        for i,point in ipairs(totalZone.verticies) do
            trigger.action.textToAll(-1,Utils.getMarkID(),point,{1,0,0,1},{0,0,0,0},14,false,"ZoneMax.verticies["..i..']')
            trigger.action.circleToAll(-1,Utils.getMarkID(),point,1000,{1,0,0,1},{1,0,0,0.3},1)
        end
    end
    -- MarkTotalZone(ZoneMax)

    local function createRangeZones(ZoneMax,rangeType,length,spacing)
        -- MarkTotalZone(ZoneMax)
        local totalLength_Horz = Utils.get2DDist(ZoneMax.verticies[1],ZoneMax.verticies[2])
        local totalLength_Vert = Utils.get2DDist(ZoneMax.verticies[2],ZoneMax.verticies[3])
        local heading = math.deg(Utils.getDirection(ZoneMax.verticies[2],ZoneMax.verticies[1]))
        local point = Utils.deepCopy(ZoneMax.verticies[1])
        local perLength = length or AGRange.getDefaultRangeRadius()
        local spacing = spacing or AGRange.getDefaultRangeSpacing()

        for i=0,math.floor(totalLength_Horz/(perLength+spacing))-1,1 do
            local startPoint = point
            startPoint = Utils.vecTranslate(startPoint,heading-90,(perLength+spacing)*i)
            startPoint = Utils.vecTranslate(startPoint,heading,(perLength+spacing)/2)
            startPoint = Utils.vecTranslate(startPoint,heading-90,(perLength+spacing)/2)
            for j=1,math.floor(totalLength_Vert/(perLength+spacing)),1 do
                AGRange:New({point = startPoint,heading = heading,radius = length,rangeType = rangeType})
                startPoint = Utils.vecTranslate(startPoint,heading,(perLength+spacing))
            end
        end
    end

    createRangeZones(PlanRangeZone)
    createRangeZones(HeliRangeZone,'Heli',3000,333)
    
--Init template
    for _,typeName in pairs(AGRange.getRangeType()) do
        for i=1,50,1 do
            local tempGroupName = 'Blue-'..typeName..'-Template-'..i
            local result = AGRange.AddTargetType(tempGroupName,typeName,coalition.side.BLUE)
            if not result then break end
        end

        for i=1,50,1 do
            local tempGroupName = 'Red-'..typeName..'-Template-'..i
            local result = AGRange.AddTargetType(tempGroupName,typeName,coalition.side.RED)
            if not result then break end
        end
    end

end

-- -- The following will use multiple functions, starting with coord.MGRStoLL to convert the values to LL which then converts them to LO, 
-- -- which will be used to draw a flag oriented with the MGRS grids over a location of no significance on the coast of the Black Sea.
-- local colors = {{0.894, 0.012, 0.012, .8},{1, 0.549, 0, .8},{1, 0.929, 0, .8},{0, 0.502, 0.149, .8},{0.141, 0.251, 0.557, .8},{0.451, 0.161, 0.51, .8}}
-- local wMod = {-1, 1, 1, -1}
-- local hMod = {1, 1, -1, -1}
-- local mgrs = {UTMZone = "37T", MGRSDigraph = "DK",}
-- local w = -15000
-- local h = -1000
-- local startEast = 40000 
-- for i = 1, 6 do
--     local startNorth = 29000 + (h * i * 2)
--     local tbl = {-1, Utils.getMarkID()}
--     for j = 1, 4 do
--         mgrs.Easting = startEast + (w * wMod[j])
--         mgrs.Northing = startNorth + (h * hMod[j])
--         local lat, lon = coord.MGRStoLL(mgrs)
--         local point = coord.LLtoLO(lat, lon, 0)
--         table.insert(tbl,point)
--     end
--     table.insert(tbl, {0, 0, 0, 0} )   
--     table.insert(tbl, colors[i])
--     table.insert(tbl,  math.random(0, 6))
--     trigger.action.quadToAll(unpack(tbl))
-- end

-- world.weather.setFogAnimation({
--     {5, 8000, 1200}, -- 8000能见度 1200厚度就会达标CASE 3
--     -- {80, 8000, 3000},
--     -- {140, 0, 0},
--   })